package com.zrc.superframe_net.interceptor

import com.zrc.superframe_net.common.download.NetDownloadProgressCallBack
import com.zrc.superframe_net.common.download.NetDownloadProgressResponseBody
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
class ProgressInterceptor(private val callBack: NetDownloadProgressCallBack) : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val originalResponse = chain.proceed(request)
        return originalResponse.newBuilder()
            .body(
                originalResponse.body?.let {
                    NetDownloadProgressResponseBody(
                        request.url.toString(),
                        it,
                        callBack
                    )
                }
            )
            .build()
    }
}